package com.yuanjiu.controller;

import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.yuanjiu.pojo.yuanjiu.CategoryIdName;
import com.yuanjiu.pojo.yuanjiu.CategoryObject;
import com.yuanjiu.service.yuanjiu.CategoryService;
import com.yuanjiu.service.yuanjiu.HomeService;
import com.yuanjiu.service.yuanjiu.ProductService;
import com.yuanjiu.vo.yuanjiu.PageVo;
import com.yuanjiu.vo.yuanjiu.ProductVo;

@Controller
public class CategoryController {

	@Autowired
	private HomeService homeService;
	@Autowired
	private CategoryService categoryService;
	@Autowired
	private ProductService productService;
	
//	@RequestMapping("productList")
//	public String productFileter(ProductVo productVo, Model model){
//		model.addAttribute("productVO", productVo);
//		
//		
//		
//		return "shop_grids";
//	}
	
	@RequestMapping("shop_grids")
	public String grids(ProductVo productVo, HttpServletRequest request, HttpSession session, Model model){
		ProductVo vo = (ProductVo) session.getAttribute("productVo");
		if(vo != null){
			if(productVo != null){
				if(StringUtils.isNotEmpty(productVo.getCid())){
					vo.setCid(productVo.getCid());
				}
				if(StringUtils.isNotEmpty(productVo.getBrand())){
					vo.setBrand(productVo.getBrand());
					//清除选项
					if("0".equals(productVo.getBrand())){
						vo.setBrand(null);
					}
				}
				if(StringUtils.isNotEmpty(productVo.getStartPrice())){
					vo.setStartPrice(productVo.getStartPrice());
				}
				if(StringUtils.isNotEmpty(productVo.getEndPrice())){
					vo.setEndPrice(productVo.getEndPrice());
					//清除选项
					if("0".equals(productVo.getEndPrice())){
						vo.setStartPrice(null);
						vo.setEndPrice(null);
					}
				}
				if(StringUtils.isNotEmpty(productVo.getStartDegree()))
				{
					vo.setStartDegree(productVo.getStartDegree());
				}
				if(StringUtils.isNotEmpty(productVo.getEndDegree())){
					vo.setEndDegree(productVo.getEndDegree());
					//清除选项
					if("0".equals(productVo.getEndDegree())){
						vo.setStartDegree(null);
						vo.setEndDegree(null);
					}
				}
				if(StringUtils.isNotEmpty(productVo.getStartNet())){
					vo.setStartNet(productVo.getStartNet());
				}
				if(StringUtils.isNotEmpty(productVo.getEndNet())){
					vo.setEndNet(productVo.getEndNet());
					//清除选项
					if("0".equals(productVo.getEndNet())){
						vo.setStartNet(null);
						vo.setEndNet(null);
					}
				}
				
				//设置查询关键词
				vo.setKeyWord(productVo.getKeyWord());
			}
			
			//清除全部选项
			String clearAll = request.getParameter("clearAll");
			if(clearAll != null && "1".equals(clearAll)){
				vo.setBrand(null);
				vo.setStartDegree(null);
				vo.setEndDegree(null);
				vo.setStartNet(null);
				vo.setEndNet(null);
				vo.setStartPrice(null);
				vo.setEndPrice(null);
			}
			
			model.addAttribute("productVo", vo);
		}else{
			session.setAttribute("productVo", productVo);
			model.addAttribute("productVo", vo);
		}
		ProductVo vo2 = (ProductVo) session.getAttribute("productVo");
		
		//获取所有的一级分类
		List<CategoryIdName> categories = homeService.findAllCategories(); 
		model.addAttribute("categoryList", categories);
		
		if(StringUtils.isNotEmpty(vo2.getCid())){
			CategoryIdName categoryIdName2= categoryService.getCategoryById(vo2.getCid());  
			model.addAttribute("categoryNow", categoryIdName2);
		}

		
		//获取此分类下所有品牌
		if(StringUtils.isNotEmpty(vo2.getCid())){
			List<CategoryObject> categoryObjectList = categoryService.findAllBrandByCategoryId(vo2.getCid()); 
			model.addAttribute("brandList", categoryObjectList);
		}
		
		//获取品牌信息
		if(StringUtils.isNotEmpty(vo2.getBrand())){
			CategoryObject categoryObject = categoryService.getBrandById(vo2.getBrand()); 
			if(categoryObject != null){
				model.addAttribute("brandInfo", categoryObject);
			}
		}
		
		//排序规则
		if(StringUtils.isNotEmpty(productVo.getOrderBy())){
			if("0".equals(productVo.getOrderBy())){
				vo2.setOrderBy(null);
			}else{
				vo2.setOrderBy(productVo.getOrderBy());
			}
		}
		if(StringUtils.isNotEmpty(productVo.getOrder())){
			vo2.setOrder(productVo.getOrder());
		}
		session.setAttribute("productVo", vo2);
		
		//查询商品总数
		PageVo voParam = new PageVo();
		voParam.setCurrentPage(1);
		voParam.setPageSize(20);
		PageVo page = productService.findProductListByFilter(vo2, voParam);
		model.addAttribute("totalCount", Math.ceil(Double.valueOf(page.getTotal())/Double.valueOf(page.getPageSize())));
		return "shop_grids";
	}
	
	/**
	 * 分页列表获取
	 * @param session
	 * @param model
	 * @return
	 */
	@RequestMapping("goodsList")
	@ResponseBody
	public PageVo goodsList(PageVo voParam, HttpServletRequest request, HttpSession session, Model model){
		ProductVo vo = (ProductVo) session.getAttribute("productVo");
		voParam.setPageSize(20);
		vo.setKeyWord(request.getParameter("keyWord"));
		PageVo page = productService.findProductListByFilter(vo, voParam); 
		return page;
	}
}
